package com.example.zjx8969.myapplication.net;

import android.text.TextUtils;
import android.util.Log;

import com.example.zjx8969.myapplication.BuildConfig;
import com.example.zjx8969.myapplication.GsonAgent;
import com.example.zjx8969.myapplication.glide.SecurityEncode;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/*public class NetInterceptor implements Interceptor {
    private final static String version = "version";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        if (originalRequest.body() == null || originalRequest.header(version) != null) {
            return chain.proceed(originalRequest);
        }
        Request.Builder builder = originalRequest.newBuilder().addHeader(version, BuildConfig.VERSION_NAME);
        if (originalRequest.body() instanceof CountingRequestBody) {
            CountingRequestBody body = (CountingRequestBody) originalRequest.body();

        } else if (!(originalRequest.body() instanceof FormBody)) {
            return chain.proceed(builder.build());
        }

        FormBody oldBody = (FormBody) originalRequest.body();
        FormBody.Builder formBodyBuilder = new FormBody.Builder();

        String requestCode = null, bizPara = null;
        for (int i = oldBody.size() - 1; i >= 0; i--) {
            if (TextUtils.isEmpty(requestCode) && TextUtils.equals("code", oldBody.name(i))) {
                requestCode = oldBody.value(i);
            } else {
                if (bizPara == null && TextUtils.equals(oldBody.name(i), "bizPara")) {
                    bizPara = oldBody.value(i);
                }
                formBodyBuilder.add(oldBody.name(i), oldBody.value(i));
            }
        }
        if (requestCode == null) {//old request
            return chain.proceed(originalRequest);
        }
        if (TextUtils.isEmpty(bizPara)) {
            bizPara = "{}";
        }
        String sysParams = GsonAgent.getInstance().gson.toJson(new BaseSysParams(requestCode));
        String md5Str = "Perfect365_Explore_H5" + "ryweurew@*&ir(==-)!&&^%@" + sysParams + bizPara;
        String sign = SecurityEncode.toMD5(md5Str);

        formBodyBuilder.add("sysPara", sysParams).add("sign", sign);
        Log.e("tag", "NetInterceptor>" + Thread.currentThread().getName() + "," + Thread.currentThread().getId());
        return chain.proceed(originalRequest.newBuilder().post(formBodyBuilder.build()).build());
    }
}*/
